package com.infore.operation.exception;

import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.FormatUtil;
import com.infore.operation.utils.StatusCode;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author:wk
 * @date:Created in 15:13 2018\4\16 0016
 */
@ControllerAdvice
public class OperationExceptionHandler {

    @ExceptionHandler(value = OperationException.class)
    @ResponseBody
    public AbstractJsonObject defaultErrorHandler(OperationException e) throws Exception {
        e.printStackTrace();
        return  FormatUtil.queryAbstractJsonObject(e.getMessage(), StatusCode.CODE_ERROR);
    }

    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public AbstractJsonObject defaultErrorHandler(MissingServletRequestParameterException e) throws Exception {
        e.printStackTrace();
        return  FormatUtil.queryAbstractJsonObject(e.getMessage().substring(e.getMessage().indexOf("'")+1,e.getMessage().lastIndexOf("'"))+"参数未传", StatusCode.CODE_ERROR);
    }
}
